package com.pansoft.openplanet.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.db.WeChatDBManager;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.mobilecomps.contacts.User;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.constant.IPAssetsConstant;
import com.pansoft.openplanet.util.EosNetParamUtil;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;


/**
 * 转账消息类型详情页
 * Created by will on 18-4-27.
 */

public class TransferMessageDetailActivity extends TCBaseActivity {

    //对方账户
    private EditText etToAccout;
    //对方地址
    private EditText etToAddress;
    private TextView tvCount, tvStatus;
    private ImageView ivIcon;
    private EditText etFromCount;
    private EditText etFromAddress;
    private EditText etTime;
    private String sendUserId;
    private EditText tradeIdEditext;
    private TextView tvTitle;
    //链编号
    private int chainId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_message_detail_transfer);
        initView();
        initData();


    }

    private void initView() {
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTitle = (TextView) findViewById(R.id.tv_title);

        ivIcon = (ImageView) findViewById(R.id.iv_icon);
        etToAccout = (EditText) findViewById(R.id.toAccout);
        etToAddress = (EditText) findViewById(R.id.toAddress);
        tvCount = (TextView) findViewById(R.id.tv_count);
        etFromCount = (EditText) findViewById(R.id.fromAccount);
        etFromAddress = (EditText) findViewById(R.id.fromAddress);
        etTime = (EditText) findViewById(R.id.et_time);
        tradeIdEditext = (EditText) findViewById(R.id.et_tradeid);
        tvStatus = (TextView) findViewById(R.id.tv_status);
    }


    private void initData() {
        //String receiveUserId = getIntent().getStringExtra("receiveUserId");

        String sendIMUserId = getIntent().getStringExtra("sendIMUserId");
        String receiveImUserId = getIntent().getStringExtra("receiveIMUserId");
        String receiveEthAddress = getIntent().getStringExtra("receiveWalletAddress");
        String sendEthAddress = getIntent().getStringExtra("sendWalletAddress");
        String money = getIntent().getStringExtra("money");
        String tradingTime = getIntent().getStringExtra("tradingTime");
        String tradingID = getIntent().getStringExtra("tradingID");

         chainId = getIntent().getIntExtra("chainId", IPAssetsConstant.CHAIN_ID_TURBOCHAIN);
        if (chainId == IPAssetsConstant.CHAIN_ID_TURBOCHAIN) {
            tvTitle.setText(getString(R.string.open_planet_exchange_record_with_type, "PWR"));
        } else if (chainId == IPAssetsConstant.CHAIN_ID_ETH) {
            tvTitle.setText(getString(R.string.open_planet_exchange_record_with_type, "ETH"));
        } else if (chainId == IPAssetsConstant.CHAIN_ID_BTC) {
            tvTitle.setText(getString(R.string.open_planet_exchange_record_with_type, "BTC"));
        } else if (chainId == EosNetParamUtil.getEosChainId()) {
            tvTitle.setText(getString(R.string.open_planet_exchange_record_with_type, "EOS"));
        }


        etTime.setText(tradingTime);
        tradeIdEditext.setText(tradingID);

//        etToAccout.setText(getFriendName(Integer.valueOf(receiveImUserId)));
//        etFromCount.setText(getFriendName(Integer.valueOf(sendIMUserId)));
//
//        etToAddress.setText(getIntent().getStringExtra("receiveWalletAddress"));
//        etFromAddress.setText(getIntent().getStringExtra("sendWalletAddress"));
        sendUserId = getIntent().getStringExtra("sendIMUserId");
//        etFromCount.setText(getFriendName(Integer.valueOf(sendUserId)));


        if ((sendUserId != null && EnvironmentVariable.getProperty(CHAT_USER_ID).equals(sendIMUserId))) {
            //转出
            tvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_transaction_result_out_success));
            ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.energy_out));
            if (chainId == IPAssetsConstant.CHAIN_ID_BTC) {
                ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.wechatview_btc_out));
            } else if (chainId == IPAssetsConstant.CHAIN_ID_ETH) {
                ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.wechatview_eth_out));
            } else if (chainId == EosNetParamUtil.getEosChainId()) {
                ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.eos_out));
            }


            tvCount.setTextColor(getResources().getColor(R.color.op_blue_color));
            tvCount.setText("-" + money);

//            etToAccout.setText(getFriendName(Integer.valueOf(receiveImUserId)));
//            etFromCount.setText(getFriendName(Integer.valueOf(sendIMUserId)));
            etToAccout.setText(receiveImUserId);
            etFromCount.setText(sendIMUserId);
            etToAddress.setText(receiveEthAddress);
            etFromAddress.setText(sendEthAddress);

        } else {
            tvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_transaction_result_in_success));
            ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.energy_in));
            if (chainId == 1) {
                ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.wechatview_btc_in));
            } else if (chainId == 2) {
                ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.wechatview_eth_in));
            } else if (chainId == EosNetParamUtil.getEosChainId()) {
                ivIcon.setImageDrawable(getResources().getDrawable(R.drawable.eos_in));
            }

            tvCount.setTextColor(getResources().getColor(R.color.op_text_yellow_color));
            tvCount.setText("+" + money);

//            etToAccout.setText(getFriendName(Integer.valueOf(sendIMUserId)));
//            etFromCount.setText(getFriendName(Integer.valueOf(receiveImUserId)));
            etToAccout.setText(sendIMUserId);
            etFromCount.setText(receiveImUserId);
            etToAddress.setText(sendEthAddress);
            etFromAddress.setText(receiveEthAddress);
        }

    }

    private String getFriendName(int userId) {
        User user = WeChatDBManager.getInstance().getOneUserById(userId);
        return user.getNickName();
    }
}
